package com.gexiaobao.pigeon.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.text.InputFilter
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isVisible
import androidx.core.widget.addTextChangedListener
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.util.TransInformation
import com.gexiaobao.pigeon.ui.view.ChineseInputFilter
import com.gexiaobao.pigeon.ui.view.EnglishInputFilter


/**
 * @author:hxw
 * @date  :2022/12/8
 * @desc  :
 */
class RxInputCustomDialog(context: Context?) : RxDialog(context!!) {

    private lateinit var cancelView: AppCompatTextView
    private lateinit var sureView: AppCompatTextView
    private lateinit var titleView: AppCompatTextView
    private lateinit var contentView: AppCompatEditText
    private lateinit var ivCleanInputContent: AppCompatImageView

    init {
        initView()
    }

    @SuppressLint("MissingInflatedId")
    private fun initView() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_input_custom, null)
        cancelView = dialogView.findViewById(R.id.tvCancel)
        sureView = dialogView.findViewById(R.id.tvSure)
        titleView = dialogView.findViewById(R.id.tvtDialogTitle)
        contentView = dialogView.findViewById(R.id.etDialogContent)
        ivCleanInputContent = dialogView.findViewById(R.id.ivCleanInputContent)

        cancelView.setOnClickListener {
            dismiss()
        }

        contentView.addTextChangedListener {
            ivCleanInputContent.isVisible = it?.isNotEmpty() == true
        }

        ivCleanInputContent.setOnClickListener {
            contentView.setText("")
        }
        setContentView(dialogView)
    }

    fun setMaxLength(num: Int) {
        val filters = arrayOf<InputFilter>(InputFilter.LengthFilter(num))
        contentView.filters = filters
    }

    fun setTransInformation() {
        contentView.transformationMethod = TransInformation()
    }

    fun setEnglishFilter() {
        contentView.filters = arrayOf(EnglishInputFilter())
    }

    fun setChineseFilter() {
        contentView.filters = arrayOf(ChineseInputFilter())
    }

    fun setHintText(hintText: String) {
        contentView.hint = hintText
    }

    fun getContent(): String {
        return contentView.text.toString().trim()
    }

    fun setContent(content: String) {
        contentView.setText(content)
        contentView.setSelection(content.length)
    }

    fun setSureListener(listener: View.OnClickListener?) {
        sureView.setOnClickListener(listener)
    }

    fun setTitle(titleStr: String) {
        titleView.text = titleStr
    }
}